import {
  Controller,
  Post,
  Body,
  UseFilters,
  UsePipes,
  UseGuards,
  ValidationPipe,
  Get,
  Req,
  Query,
  HttpException,
  HttpStatus,
  Delete,
} from '@nestjs/common'
import { UserService } from './user.service'
import { getUserInfo, loginDto } from './UserDto'
import { HttpExceptionFilter } from '../filter/httpRequestCatch'
import { AuthGuard } from '@nestjs/passport/dist'
import { Request } from 'express'
import { ReqUser } from 'src/strategy/dto/strategy.dto'

@Controller('user')
@UseFilters(HttpExceptionFilter) //异常过滤器
@UsePipes(new ValidationPipe()) //nest验证管道
export class UserController {
  constructor(private readonly userServer: UserService) {}
  //注册
  @Post('register')
  async register(@Body() userInfo: getUserInfo) {
    const result = await this.userServer.register(userInfo)

    return {
      data: result,
      message: '注册成功',
      code: 200,
    }
  }

  //登录
  @Post('login')
  async login(@Body() loginInfo: loginDto) {
    const result = await this.userServer.login(loginInfo)
    return result
  }
  //验证token
  @Get('verifyToken')
  @UseGuards(AuthGuard('jwt'))
  async getUserInfo(@Req() req: Request) {
    return {
      data: req.user,
      code: 200,
    }
  }

  /* 查询关注的贴吧列表 */
  @Get('getSubBarList')
  @UseGuards(AuthGuard('jwt'))
  async getSubBar(@Req() req: Request, @Query() query: { page: number; pagesize: number }) {
    const info = { ...query, userID: (req.user as ReqUser)._id }
    return await this.userServer.getSubBarList(info)
  }

  /* 根据用户名查询用户 */
  @Get('serch')
  async serchUser(@Query() info: { username: string; page: number; pagesize: number }) {
    return await this.userServer.findUser(info.username, info.page, info.pagesize)
  }

  /* 发送好友验证 */
  @Get('addFriend')
  @UseGuards(AuthGuard('jwt'))
  async addFriend(@Req() req: Request, @Query() info: { friend: string }) {
    const user = req.user as ReqUser
    return await this.userServer.requestFriend({ user: user._id, friend: info.friend })
  }

  /* 查询好友验证列表 */
  @Get('verifyList')
  @UseGuards(AuthGuard('jwt'))
  async getVerifyList(@Req() req: Request, @Query() info: { page: number }) {
    const user = req.user as ReqUser
    return await this.userServer.getVerifyList(user._id, info.page)
  }

  /* 同意好友申请 */
  @Get('agreeFriend')
  @UseGuards(AuthGuard('jwt'))
  async agreeFriend(@Req() req: Request, @Query() info: { verifyID: string }) {
    const user = req.user as ReqUser
    return await this.userServer
      .agreeRequest(info.verifyID, user._id)
      .catch((err) => new HttpException(err, HttpStatus.BAD_REQUEST))
  }

  /* 获取好友列表 */
  @Get('FriendList')
  @UseGuards(AuthGuard('jwt'))
  async getFriendList(@Req() req: Request) {
    const user = req.user as ReqUser
    return await this.userServer.getFriendList(user._id).catch((err) => new HttpException(err, HttpStatus.BAD_REQUEST))
  }

  /* 拒绝好友申请 | 删除好友申请记录 */
  @Get('deleteVerify')
  @UseGuards(AuthGuard('jwt'))
  async deleteFriendVerify(@Req() req: Request, @Query() info: { verifyID: string }) {
    const user = req.user as ReqUser
    return await this.userServer.rejectFriend(info.verifyID, user._id)
  }

  /* 删除好友 */
  @Delete('deleteFriend')
  @UseGuards(AuthGuard('jwt'))
  async deleteFriend(@Req() req: Request, @Query() info: { friendID: string }) {
    const user = req.user as ReqUser
    return await this.userServer.deleteFriend(user._id, info.friendID)
  }
}
